package com.k1998.mybatis.controller;

import com.alibaba.fastjson.JSON;

import com.k1998.mybatis.bean.*;
import com.k1998.mybatis.dao.IMenuDao;
import com.k1998.mybatis.utils.QueryInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = {"菜单Api文档"})
public class MenuController {
    @Autowired
    private IMenuDao imenuDao;

    @PostMapping("/menus")
    public String getAllMenus(){
        HashMap<String, Object> data = new HashMap<>();
        List<MainMenu> mainMenus = imenuDao.getMainMenus();
        data.put("data",mainMenus);
        data.put("code",200);
        String data_json = JSON.toJSONString(data);
        System.out.println("成功访问！！！");
        return data_json;
    }

    @PostMapping("/getSubMenus")
    public String getSubMenus(@RequestBody QueryInfo queryInfo) {
        // 查询到的结果数量
        int numbers = imenuDao.getCounts("%"+queryInfo.getQuery()+"%");
        // 第几条数据开始
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<SubMenu> subMenu = imenuDao.getSubMenus("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",subMenu);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/insertSubMenus")
    @ApiOperation(value = "添加admin信息", notes="添加admin信息", httpMethod = "POST")
    public String insertSubMenus(@RequestBody SubMenu subMenu){
        int i = imenuDao.insertSubMenus(subMenu);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/detailSubMenus")
    public String detailSubMenus(@RequestBody SubMenu subMenu){
        SubMenu s = imenuDao.detailSubMenus(subMenu);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",s);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/updateSubMenus")
    @ApiOperation(value = "修改admin信息", notes="修改admin信息", httpMethod = "POST")
    public String updateSubMenus(@RequestBody SubMenu subMenu){
        int i = imenuDao.updateSubMenus(subMenu);
        String str = i >0?"success":"error";
        return str;
    }

}
